package com.tg.app.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.joooonho.SelectableRoundedImageView;
import com.tg.app.R;
import com.tg.app.adapter.PrePositionAdapter;
import com.tg.app.view.LengthFilter;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGToast;

public class PrePositionCreateDialog {
    private final Context mContext;
    private Dialog mDialog;
    private SelectableRoundedImageView imageView;
    EditText editText;
    private PrePositionCreateDialogOnSubmitListener listener;

    public void setListener(@NonNull PrePositionCreateDialogOnSubmitListener listener) {
        this.listener = listener;
    }

    public PrePositionCreateDialog(@NonNull Context context) {
        this.mContext = context;
    }

    @NonNull
    public PrePositionCreateDialog builder(@NonNull String from) {
        @LayoutRes int resource = R.layout.dialog_pre_position_create;
        if (StringUtils.equalsIgnoreCase(from, PrePositionAdapter.MODE_SWITCH)){
            resource = R.layout.dialog_pre_position_create_from_fragment;
        }
        @SuppressLint("InflateParams") View view = LayoutInflater.from(mContext).inflate(resource, null);
        LinearLayout mLLayout = view.findViewById(R.id.lLayout_bg);
        imageView = view.findViewById(R.id.dialog_pre_position_img);
        editText = view.findViewById(R.id.per_position_create_name_edt);
        mDialog = new Dialog(mContext, R.style.alert_dialog);
        mDialog.setContentView(view);
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        editText.setFilters(new InputFilter[]{new LengthFilter(10)});
        mLLayout.setLayoutParams(new FrameLayout.LayoutParams((int) (displayMetrics.widthPixels * 0.80), LinearLayout.LayoutParams.WRAP_CONTENT));
        view.findViewById(R.id.btn_neg).setOnClickListener(v -> mDialog.dismiss());
        view.findViewById(R.id.btn_pos).setOnClickListener(v -> {
            String text = editText.getText().toString().trim();
            if (TextUtils.isEmpty(text)) {
                TGToast.showToast(R.string.bottom_sheet_pre_position_name_empty);
                return;
            }
            mDialog.dismiss();
            if (listener != null) {
                listener.onClick(text);
            }

        });
        return this;
    }

    @NonNull
    public PrePositionCreateDialog setEditText(@NonNull String text) {
        editText.setText(text);
        return this;
    }

    public void show() {
        if (mContext instanceof Activity) {
            if (((Activity) mContext).isFinishing()) return;
        }

        mDialog.show();
    }

    public void setImageBitmap(@NonNull Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
    }

    public interface PrePositionCreateDialogOnSubmitListener {
        void onClick(@NonNull String text);
    }

}
